/*
	Copyright (c) 2010 Canadensys
*/
package net.canadensys.dataportal.utils;

import net.canadensys.dataportal.scientificname.ScientificNameStructure;
import net.canadensys.dataportal.scientificname.TaxonRankEnum;

public class FullScientificNameGenerator {
	
	public static String encode(ScientificNameStructure scientificNameStructure, String authority, boolean withAuthor, boolean withHtml){
		String uninomial = scientificNameStructure.getUninomial();
		String binomial = scientificNameStructure.getBinomial();
		String trinomial = scientificNameStructure.getTrinomial();
		String quadrinomial = scientificNameStructure.getQuadrinomial();
		TaxonRankEnum taxonRank = scientificNameStructure.getRankEnum();
		
		String fullScientificName = "";
		//TODO switch on the enum
		switch(scientificNameStructure.getRankId()){
			//from root till genus
			case 1 : ;
			case 2 : ;
			case 3 : ;
			case 4 : ;
			case 5 : ;
			case 6 : ;
			case 7 : ;
			case 8 : ;
			case 9 : 
				if(withHtml)
					fullScientificName = "<em>" + uninomial + "</em>";
				else
					fullScientificName = uninomial;				
				if(withAuthor)
					fullScientificName += " " + authority; 
				break;
			//subgenus, section, subsection, series
			case 10 : ; 
			case 11 : ;
			case 12 : ;
			case 13 : 
				//fullscientificname += " " + getRank().getRank() + " " + generateFullName();
				if(withHtml)
					fullScientificName = "<em>" + uninomial + "</em> " + taxonRank.getAbbreviation()  + " <em>" + binomial + "</em>";
				else
					fullScientificName = uninomial + " " + taxonRank.getAbbreviation()  + " " + binomial;
				if(withAuthor)
					fullScientificName += " " + authority;
				break;
			//species
			case 14 : 
				if(withHtml)
					fullScientificName = "<em>" + uninomial + " " + binomial + "</em>";
				else
					fullScientificName = uninomial + " " + binomial;
				if(withAuthor)
					fullScientificName += " " + authority;
				break;
			//subspecies
			case 15 :
				if(withAuthor){
					if(!binomial.equals(trinomial)){
						if(withHtml)
							fullScientificName = "<em>" + uninomial + " " + binomial + "</em> subsp. <em>" + trinomial + "</em> " + authority;
						else
							fullScientificName = uninomial + " " + binomial + " subsp. " + trinomial + " " + authority;
					}
					else{
						if(withHtml)
							fullScientificName = "<em>" + uninomial + " " + binomial + "</em> " + authority + " subsp. <em>" + trinomial + "</em>";
						else
							fullScientificName = uninomial + " " + binomial + " " + authority + " subsp. " + trinomial;
					}
				}
				else{
					if(withHtml)
						fullScientificName = "<em>" + uninomial + " " + binomial + "</em> subsp. <em>" + trinomial + "</em>";
					else
						fullScientificName = uninomial + " " + binomial + " subsp. " + trinomial;
				}
				break;
			//variety
			case 16 :
				//genus, species, subspecies, variery
				if(quadrinomial != null && !quadrinomial.equals("")){
					if(withAuthor){
						if(withHtml){
							if(!quadrinomial.equals(trinomial))
								fullScientificName = "<em>" + uninomial + " " + binomial + "</em> subsp. <em>" + trinomial + "</em> var. <em>" + quadrinomial + "</em> " + authority;
							else if(!trinomial.equals(binomial))
								fullScientificName = "<em>" + uninomial + " " + binomial + "</em> subsp. <em>" + trinomial + " " + authority + "</em> var. <em>" + quadrinomial + "</em>";
							else// if(trinomial.equals(binomial))
								fullScientificName = "<em>" + uninomial + " " + binomial + "</em> " + authority + " subsp. <em>" + trinomial + "</em> var. <em>" + quadrinomial + "</em>";
						}
						else{
							if(!quadrinomial.equals(trinomial))
								fullScientificName = uninomial + " " + binomial + " subsp. " + trinomial + " var. " + quadrinomial + " " + authority;
							else if(!trinomial.equals(binomial))
								fullScientificName = uninomial + " " + binomial + " subsp. " + trinomial + " " + authority + " var. " + quadrinomial;
							else// if(trinomial.equals(binomial))
								fullScientificName = uninomial + " " + binomial + " " + authority + " subsp. " + trinomial + " var. " + quadrinomial;							
						}
					}
					else{
						if(withHtml)
							fullScientificName = "<em>" + uninomial + " " + binomial + "</em> subsp. <em>" + trinomial + "</em> var. <em>" + quadrinomial + "</em>";
						else
							fullScientificName = uninomial + " " + binomial + " subsp. " + trinomial + " var. " + quadrinomial;
					}
				}
				else{
					if(withAuthor){
						if(withHtml){
							if(trinomial.equals(binomial))
								fullScientificName = "<em>" + uninomial + " " + binomial + "</em> " + authority + " var. <em>" + trinomial + "</em>";
							else
								fullScientificName = "<em>" + uninomial + " " + binomial + "</em> var. <em>" + trinomial + "</em> " + authority;
						}
						else{
							if(trinomial.equals(binomial))
								fullScientificName = uninomial + " " + binomial + " " + authority + " var. " + trinomial;
							else
								fullScientificName = uninomial + " " + binomial + " var. " + trinomial + " " + authority;
						}
					}
					else{
						if(withHtml)
							fullScientificName = "<em>" + uninomial + " " + binomial + "</em> var. <em>" + trinomial + "</em>";
						else
							fullScientificName = uninomial + " " + binomial + " var. " + trinomial;
					}
				}
				break;
		
		}
		//trim the result the remove trailing spaces
		return fullScientificName.trim();
	}
}
